/*
 * Created by SharpDevelop.
 * User: c0148643
 * Date: 11/05/2010
 * Time: 11:12
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */

using System;
using System.Data;
using System.Data.SQLite;
using System.Collections.Generic;

namespace pontoIndividual
{

	public class configuracao
	{
		private estadoConfig mapearEventoWindows;
		public estadoConfig MapearEventoWindows {
			get { return mapearEventoWindows; }
			set { mapearEventoWindows = value; }
		}
		
		private estadoConfig avisarHorarios;		
		public estadoConfig AvisarHorarios {
			get { return avisarHorarios; }
			set { avisarHorarios = value; }
		}
		private estadoConfig habilitarBackup;		
		public estadoConfig HabilitarBackup {
			get { return habilitarBackup; }
			set { habilitarBackup = value; }
		}
		
		private string horarioAlmoco;		
		public string HorarioAlmoco {
			get { return horarioAlmoco; }
			set { horarioAlmoco = value; }
		}
		private string horarioSaida;		
		public string HorarioSaida {
			get { return horarioSaida; }
			set { horarioSaida = value; }
		}
		private string horarioBackup;		
		public string HorarioBackup {
			get { return horarioBackup; }
			set { horarioBackup = value; }
		}
		
		private string pastaBackup;		
		public string PastaBackup {
			get { return pastaBackup; }
			set { pastaBackup = value; }
		}
		private string pastaBackupRede;		
		public string PastaBackupRede {
			get { return pastaBackupRede; }
			set { pastaBackupRede = value; }
		}
		private string pastaBackupPendrive;		
		public string PastaBackupPendrive {
			get { return pastaBackupPendrive; }
			set { pastaBackupPendrive = value; }
		}
		
		private string pastaBackupDocumentacao;	
		public string PastaBackupDocumentacao {
			get { return pastaBackupDocumentacao; }
			set { pastaBackupDocumentacao = value; }
		}
		
		
		public configuracao()
		{
			// 
			loadAllConfiguracoes();
		}
		
		public string loadConfiguracao(string nomeConfig) {
			string res = "";
			
			using (conexao c = new conexao())
			{
				c.open();
				
				SQLiteCommand sc = new SQLiteCommand("SELECT valor FROM configuracao WHERE nome = @nome", c.conn);
				sc.Parameters.AddWithValue("@nome", nomeConfig);

				res = sc.ExecuteScalar().ToString();				
			}
			
			return res;
		}
		public string loadConfiguracao(string nomeConfig, conexao con) {
			object res;

			con.open();
			
			SQLiteCommand sc = new SQLiteCommand("SELECT valor FROM configuracao WHERE nome = @nome", con.conn);
			sc.Parameters.AddWithValue("@nome", nomeConfig);

			res = sc.ExecuteScalar();
			
			return (res == null ? "" : res.ToString());
		}
		public void saveConfiguracao(string nomeConfig, string valor) {
			using (conexao c = new conexao()) {
				c.open();
				
				SQLiteCommand sc = new SQLiteCommand("UPDATE configuracao SET valor = @valor WHERE nome = @nome", c.conn);
				sc.Parameters.AddWithValue("@valor", valor);
				sc.Parameters.AddWithValue("@nome", nomeConfig);
				sc.ExecuteNonQuery();				
			}
			
		}
		public void saveConfiguracao(string nomeConfig, string valor, conexao con) {
			
			SQLiteCommand sc = new SQLiteCommand("UPDATE configuracao SET valor = @valor WHERE nome = @nome", con.conn);
			sc.Parameters.AddWithValue("@valor", valor);
			sc.Parameters.AddWithValue("@nome", nomeConfig);
			sc.ExecuteNonQuery();
			
		}
		public void saveConfiguracao(string nomeConfig, estadoConfig valor, conexao con) {
			
			SQLiteCommand sc = new SQLiteCommand("UPDATE configuracao SET valor = @valor WHERE nome = @nome", con.conn);
			sc.Parameters.AddWithValue("@valor", valor == estadoConfig.Habilitado ? "1" : "0");
			sc.Parameters.AddWithValue("@nome", nomeConfig);
			sc.ExecuteNonQuery();
			
		}		
		public void loadAllConfiguracoes() {
			
			using (conexao c = new conexao()) {
			    
				c.open();
				
				mapearEventoWindows = loadConfiguracao("mapearEventoWindows", c) == "1" ? estadoConfig.Habilitado : estadoConfig.Desabilitado;
				avisarHorarios = loadConfiguracao("avisarHorarios", c) == "1" ? estadoConfig.Habilitado : estadoConfig.Desabilitado;
				habilitarBackup = loadConfiguracao("habilitarBackup", c) == "1" ? estadoConfig.Habilitado : estadoConfig.Desabilitado;
				horarioAlmoco = loadConfiguracao("horarioAlmoco", c);
				horarioSaida = loadConfiguracao("horarioSaida", c);
				horarioBackup = loadConfiguracao("horarioBackup", c);
				pastaBackup = loadConfiguracao("pastaBackup", c);
				pastaBackupRede = loadConfiguracao("pastaBackupRede", c);
				pastaBackupPendrive = loadConfiguracao("pastaBackupPendrive", c);
				pastaBackupDocumentacao = loadConfiguracao("pastaBackupDocumentacao", c);
				
		    }
		}
		
		
		
		public void gravar()
		{

			using (conexao c = new conexao())
			{
				c.open();

				saveConfiguracao("mapearEventoWindows", mapearEventoWindows, c);
				saveConfiguracao("habilitarBackup", habilitarBackup, c);
				saveConfiguracao("avisarHorarios", avisarHorarios, c);
				saveConfiguracao("horarioAlmoco", horarioAlmoco, c);
				saveConfiguracao("horarioSaida", horarioSaida, c);
				saveConfiguracao("horarioBackup", horarioBackup, c);
				saveConfiguracao("pastaBackup", pastaBackup, c);
				saveConfiguracao("pastaBackupRede", pastaBackupRede, c);
				saveConfiguracao("pastaBackupPendrive", pastaBackupPendrive, c);
				saveConfiguracao("pastaBackupDocumentacao", pastaBackupDocumentacao, c);				
			}
		}
	}


	public enum estadoConfig
	{
		Desabilitado,
		Habilitado
	}
}
